package com.cx.controller;

import com.cx.pojo.Address;
import com.cx.pojo.Carts;
import com.cx.pojo.Users;
import com.cx.service.OrderService;
import com.cx.vo.CartsInfo;
import com.cx.service.CartService;
import com.cx.service.CartsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @Description
 * @Author 11597
 * @Date 2020/5/12 14:53
 */
@Controller
@RequestMapping("cart")
public class CartController {
    @Autowired
    private CartsInfoService cartsInfoService;

    @Autowired
    private CartService cartService;

    @Autowired
    private OrderService orderService;



    /**
     * 显示用户购物车中的商品
     * @param session
     * @return
     */
    @RequestMapping("showcart")
    public ModelAndView showCart(HttpSession session){
        Users user = (Users) session.getAttribute("user");
        ModelAndView modelAndView = new ModelAndView();
        if (user == null) {
            modelAndView.setViewName("redirect:/index/login");
            return modelAndView;
        }
        List<CartsInfo> cartsInfoList = cartsInfoService.selectByUserId(user.getId());
        Double totalPrice = cartsInfoService.getTotalPrice(user.getId());
        System.out.println(totalPrice);
        modelAndView.addObject("totalPrice",totalPrice);
        modelAndView.addObject("cartsInfoList",cartsInfoList);
        modelAndView.setViewName("cart/cart");
        //转发到jsp
        return modelAndView;
    }


    /***
     * 购物车中商品数量增加
     * @param skuid
     * @return
     */
    @RequestMapping("/add")
    public @ResponseBody int add(int skuid){
        return cartService.add(skuid);
    }

    /**
     * 购物车中商品数量减少
     * @param skuid
     * @return
     */
    @RequestMapping("less")
    public  @ResponseBody int less(int skuid){
        return cartService.less(skuid);
    }

    /**
     * 购物车中删除商品
     * @param skuid
     * @param session
     * @return
     */
    @RequestMapping("/delete")
    public @ResponseBody boolean delete(int skuid, HttpSession session){
        Users user = (Users) session.getAttribute("user");
        if (user == null) {
            return false;
        }
        cartService.delete(skuid);
        session.setAttribute("total", cartsInfoService.getTotalPrice(user.getId()));
        return true;
    }

    /**
     * 购物车中商品总价格
     * @param session
     * @return
     */
    @RequestMapping("/total")
    public @ResponseBody double total(HttpSession session){
        Users user = (Users) session.getAttribute("user");
        if (user == null) {
            return -111;
        }
        System.out.println(cartsInfoService.getTotalPrice(user.getId()));
        return cartsInfoService.getTotalPrice(user.getId());
    }


    /**
     * 购物车下单确定库存
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public Map<Object,Object> save(HttpSession session,String checkList) {
        Map<Object,Object> map = new HashMap<>();
        String[] strings = checkList.split(",");
        Integer[] goodIds=new Integer[strings.length];
        boolean flag = true;
        for (int i = 0; i < strings.length; i++) {
            goodIds[i] =Integer.parseInt(strings[i]);
        }
        Users user = (Users) session.getAttribute("user");
        if (user == null) {
            map.put("code",0);
            return map;
        }
        StringBuffer msg = new StringBuffer() ;
        for (int i = 0; i < goodIds.length; i++) {
            CartsInfo cartsInfo = cartsInfoService.selectByUserIdAndGoodId(user.getId(),goodIds[i]);
            if (cartsInfo.getGoodNum()>cartsInfo.getGoodStock()){
                flag = false;
                msg.append(cartsInfo.getGoodName());
            }
        }
        if (flag == false){
            map.put("code",1);
            map.put("msg",msg);
        } else{
            map.put("code",2);
        }
        return map;
    }


    /**
     * 购物车提交订单
     * @return
     */
    @RequestMapping("/cartOrder")
    public ModelAndView cartOrder(HttpSession session, @RequestParam("checkList") String checkList, ModelAndView modelAndView,Double totalprice) {
        System.out.println(totalprice);
        Users user = (Users) session.getAttribute("user");
        String[] strings = checkList.split(",");
        Integer[] goodIds=new Integer[strings.length];
        for (int i = 0; i < strings.length; i++) {
            goodIds[i] =Integer.parseInt(strings[i]);
            System.out.println(goodIds[i]);
        }
        List<Carts> cartsList = new ArrayList<>();
        List<CartsInfo> cartsInfoList = new ArrayList<>();
        for (int i = 0; i < goodIds.length; i++) {
            Carts carts = cartService.selectByUserIdAndGoodId(user.getId(),goodIds[i]);
            CartsInfo cartsInfo = cartsInfoService.selectByUserIdAndGoodId(user.getId(),goodIds[i]);
            cartsList.add(carts);
            cartsInfoList.add(cartsInfo);
        }
        for(CartsInfo cartsInfo : cartsInfoList){
            System.out.println(cartsInfo);
        }
        int orderId = cartsInfoService.addCartsOrder(cartsList,user.getId(),totalprice);
        List<Address> addressList = orderService.selectByUserId(user.getId());

        modelAndView.addObject("addressList",addressList);
        modelAndView.addObject("orderId",orderId);
        modelAndView.addObject("cartsInfoList",cartsInfoList);
        modelAndView.addObject("totalprice",totalprice);
        modelAndView.setViewName("orderDetail");
        return modelAndView;
    }
}
